<?php
session_start();
require_once '../config.php';
require_once '../functions.php';

// 检查管理员是否登录
if (!is_admin_logged_in()) {
    header('Location: login.php');
    exit;
}

$message = '';
$message_type = '';

// 处理操作
if (isset($_GET['action'])) {
    $id = $_GET['id'] ?? 0;
    if (!is_numeric($id)) {
        $message = '参数错误';
        $message_type = 'error';
    } else {
        $id = (int)$id;
        switch ($_GET['action']) {
            case 'delete':
                // 删除评论
                try {
                    $stmt = $pdo->prepare("DELETE FROM comments WHERE id = ?");
                    $stmt->execute([$id]);
                    $message = '删除成功';
                    $message_type = 'success';
                } catch (PDOException $e) {
                    $message = '删除失败，请重试';
                    $message_type = 'error';
                }
                break;
                
            case 'ban':
                // 封禁IP
                try {
                    $stmt = $pdo->prepare("SELECT ip_address FROM comments WHERE id = ?");
                    $stmt->execute([$id]);
                    $result = $stmt->fetch();
                    
                    if ($result) {
                        $ip = $result['ip_address'];
                        $reason = "发布违规评论（ID: $id）";
                        
                        // 检查IP是否已被封禁
                        $stmt = $pdo->prepare("SELECT id FROM banned_ips WHERE ip_address = ?");
                        $stmt->execute([$ip]);
                        
                        if ($stmt->rowCount() == 0) {
                            $stmt = $pdo->prepare("INSERT INTO banned_ips (ip_address, reason, banned_by) VALUES (?, ?, ?)");
                            $stmt->execute([$ip, $reason, $_SESSION['admin_id']]);
                            $message = "IP $ip 已被封禁";
                            $message_type = 'success';
                        } else {
                            $message = "IP $ip 已在封禁列表中";
                            $message_type = 'info';
                        }
                    } else {
                        $message = '评论不存在';
                        $message_type = 'error';
                    }
                } catch (PDOException $e) {
                    $message = '封禁失败，请重试';
                    $message_type = 'error';
                }
                break;
                
            default:
                $message = '无效操作';
                $message_type = 'error';
                break;
        }
    }
}

// 筛选评论
$filter_type = $_GET['type'] ?? 'all';
$where = '';

if ($filter_type === 'confession') {
    $where = "WHERE type = 'confession'";
} elseif ($filter_type === 'daily') {
    $where = "WHERE type = 'daily'";
}

// 搜索功能
$search = $_GET['search'] ?? '';
if (!empty($search)) {
    $searchTerm = "%{$search}%";
    $where .= $where ? " AND content LIKE ?" : "WHERE content LIKE ?";
    
    // 获取评论列表（带搜索）
    $stmt = $pdo->prepare("SELECT * FROM comments $where ORDER BY created_at DESC");
    $stmt->execute([$searchTerm]);
} else {
    // 获取评论列表
    $stmt = $pdo->query("SELECT * FROM comments $where ORDER BY created_at DESC");
}
$comments = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>评论管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="../static/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/all.min.css">
    <style>
        body {
            background-color: #f8f9fa;
            font-family: 'Arial', sans-serif;
        }
        a {
            text-decoration: none;
            outline: none;
        }
        .sidebar {
            width: 250px; /* 固定宽度 */
            height: 100vh;
            background-color: #ff8fab;
            color: white;
            padding-top: 20px;
            position: fixed;
            overflow-y: auto;
            transition: transform 0.3s ease; /* 添加过渡效果 */
            z-index: 100; /* 确保在内容上方 */
        }
        
        .sidebar-header {
            text-align: center;
            padding-bottom: 20px;
            border-bottom: 1px solid rgba(255,255,255,0.2);
            margin-bottom: 20px;
        }
        
        .sidebar-header h2 {
            margin: 0;
            font-size: 1.5rem;
        }
        
        .nav-link {
            color: white;
            padding: 10px 20px;
            display: flex;
            align-items: center;
            gap: 10px;
            transition: all 0.3s;
            border-left: 3px solid transparent;
        }
        
        .nav-link:hover, .nav-link.active {
            background-color: rgba(255,255,255,0.1);
            color: white;
            text-decoration: none;
            border-left-color: #ffd700;
        }
        
        .main-content {
            margin-left: 250px;
            padding: 30px;
            transition: margin-left 0.3s ease; /* 添加过渡效果 */
            min-height: 100vh;
        }
        
        .page-title {
            color: #d63384;
            margin-bottom: 30px;
            font-size: 1.8rem;
        }
        
        .filter-bar {
            margin-bottom: 20px;
            display: flex;
            gap: 10px;
            flex-wrap: wrap;
            align-items: center;
        }
        
        .search-box {
            margin-left: auto;
            display: flex;
            gap: 5px;
        }
        
        .search-input {
            padding: 8px 15px;
            border-radius: 20px;
            border: 1px solid #ddd;
            width: 250px;
        }
        
        .search-btn {
            padding: 8px 15px;
            border-radius: 20px;
            border: none;
            background-color: #ff69b4;
            color: white;
            cursor: pointer;
        }
        
        .filter-btn {
            padding: 8px 15px;
            border-radius: 20px;
            border: 1px solid #ff69b4;
            background-color: <?php echo $filter_type === 'all' ? '#ff69b4' : 'white'; ?>;
            color: <?php echo $filter_type === 'all' ? 'white' : '#ff69b4'; ?>;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .filter-btn:hover {
            background-color: #ff69b4;
            color: white;
        }
        
        .filter-btn.confession {
            background-color: <?php echo $filter_type === 'confession' ? '#ff69b4' : 'white'; ?>;
            color: <?php echo $filter_type === 'confession' ? 'white' : '#ff69b4'; ?>;
        }
        
        .filter-btn.daily {
            background-color: <?php echo $filter_type === 'daily' ? '#ff69b4' : 'white'; ?>;
            color: <?php echo $filter_type === 'daily' ? 'white' : '#ff69b4'; ?>;
        }
        
        .table-container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.05);
            overflow: hidden;
        }
        
        .data-table {
            width: 100%;
            border-collapse: collapse;
        }
        
        .data-table th, .data-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #eee;
        }
        
        .data-table th {
            background-color: #fff0f3;
            color: #d63384;
            font-weight: bold;
        }
        
        .data-table tr:hover {
            background-color: #f9f9f9;
        }
        
        .type-badge {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 12px;
            font-size: 0.8rem;
            font-weight: bold;
        }
        
        .badge-confession {
            background-color: #ffb6c1;
            color: #8b0000;
        }
        
        .badge-daily {
            background-color: #98fb98;
            color: #006400;
        }
        
        .action-btn {
            padding: 5px 10px;
            border-radius: 5px;
            border: none;
            cursor: pointer;
            margin-right: 5px;
            font-size: 0.9rem;
            transition: background-color 0.3s;
            text-decoration: none;
            display: inline-block;
        }
        
        .btn-delete {
            background-color: #dc3545;
            color: white;
        }
        
        .btn-delete:hover {
            background-color: #c82333;
            color: white;
        }
        
        .btn-ban {
            background-color: #fd7e14;
            color: white;
        }
        
        .btn-ban:hover {
            background-color: #e0a800;
            color: white;
        }
        
        .message {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
        }
        
        .message-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .message-info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        
        .logout-btn {
            margin-top: 20px;
            text-align: center;
        }
        
        .logout-btn a {
            color: white;
            text-decoration: none;
            display: block;
            padding: 10px;
            background-color: rgba(255,255,255,0.1);
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        
        .logout-btn a:hover {
            background-color: rgba(255,255,255,0.2);
        }
        
        @media (max-width: 768px) {
            .sidebar {
                width: 100%;
                height: auto;
                position: relative;
            }
            
            .main-content {
                margin-left: 0;
                padding: 20px;
            }
            
            .filter-bar {
                flex-direction: column;
                align-items: flex-start;
            }
            
            .search-box {
                margin-left: 0;
                width: 100%;
            }
            
            .search-input {
                width: 100%;
            }
            
            .data-table th:nth-child(5), 
            .data-table td:nth-child(5) {
                display: none;
            }
        }
    </style>
</head>
<body>
    <div class="sidebar col-md-3 col-lg-2 d-md-block">
        <div class="sidebar-header">
            <h2><?php echo SITE_NAME; ?> 管理后台</h2>
        </div>
        <ul class="nav flex-column">
            <li class="nav-item">
                <a class="nav-link" href="index.php">
                    <i class="fas fa-tachometer-alt"></i> 仪表盘
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="confessions.php">
                    <i class="fas fa-heart"></i> 表白管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="dailies.php">
                    <i class="fas fa-coffee"></i> 日常管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="announcements.php">
                    <i class="fas fa-bullhorn"></i> 公告管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link active" href="comments.php">
                    <i class="fas fa-comments"></i> 评论管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="prohibited_words.php">
                    <i class="fas fa-ban"></i> 违禁词管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="banned_ips.php">
                    <i class="fas fa-shield-alt"></i> 封禁管理
                </a>
            </li>
            <li class="nav-item">
                <a class="nav-link" href="settings.php">
                    <i class="fas fa-cog"></i> 系统设置
                </a>
            </li>
        </ul>
        
        <div class="logout-btn">
            <a href="logout.php">
                <i class="fas fa-sign-out-alt"></i> 退出登录
            </a>
        </div>
    </div>
    
    <main class="main-content">
        <h1 class="page-title"><i class="fas fa-comments"></i> 评论管理</h1>
        
        <?php if (!empty($message)): ?>
            <div class="message message-<?php echo $message_type; ?>">
                <?php echo $message; ?>
            </div>
        <?php endif; ?>
        
        <div class="filter-bar">
            <a href="comments.php?type=all" class="filter-btn">全部评论</a>
            <a href="comments.php?type=confession" class="filter-btn confession">表白评论</a>
            <a href="comments.php?type=daily" class="filter-btn daily">日常评论</a>
            
            <div class="search-box">
                <form method="get" action="comments.php">
                    <input type="hidden" name="type" value="<?php echo $filter_type; ?>">
                    <input type="text" name="search" class="search-input" placeholder="搜索评论内容..." value="<?php echo htmlspecialchars($search); ?>">
                    <button type="submit" class="search-btn"><i class="fas fa-search"></i></button>
                </form>
            </div>
        </div>
        
        <div class="table-container">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>类型</th>
                        <th>关联ID</th>
                        <th>内容</th>
                        <th>IP地址</th>
                        <th>发布时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (count($comments) > 0): ?>
                        <?php foreach ($comments as $comment): ?>
                            <tr>
                                <td><?php echo $comment['id']; ?></td>
                                <td>
                                    <?php if ($comment['type'] === 'confession'): ?>
                                        <span class="type-badge badge-confession">表白评论</span>
                                    <?php else: ?>
                                        <span class="type-badge badge-daily">日常评论</span>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo $comment['target_id']; ?></td>
                                <td><?php echo htmlspecialchars($comment['content']); ?></td>
                                <td><?php echo $comment['ip_address']; ?></td>
                                <td><?php echo date('Y-m-d H:i', strtotime($comment['created_at'])); ?></td>
                                <td>
                                    <a href="comments.php?action=ban&id=<?php echo $comment['id']; ?>&type=<?php echo $filter_type; ?><?php echo !empty($search) ? "&search=" . urlencode($search) : ""; ?>" class="action-btn btn-ban" onclick="return confirm('确定要封禁此IP吗？')">
                                        <i class="fas fa-ban"></i> 封禁IP
                                    </a>
                                    <a href="comments.php?action=delete&id=<?php echo $comment['id']; ?>&type=<?php echo $filter_type; ?><?php echo !empty($search) ? "&search=" . urlencode($search) : ""; ?>" class="action-btn btn-delete" onclick="return confirm('确定要删除吗？此操作不可恢复！')">
                                        <i class="fas fa-trash"></i> 删除
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="7" style="text-align: center; padding: 30px;">
                                暂无评论数据
                            </td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </main>
</body>
</html>
